<template>
  <div class="changePassword">
    <van-nav-bar
      title="设置新密码"
      left-text="返回"
      :fixed="true"
      left-arrow
      @click-left="back"
    ></van-nav-bar>
    <div class="content">
      <van-cell-group>
        <van-field v-model="usedPassword" type="password" label="旧密码" />
        <van-field v-model="newPassword" type="password" label="新密码" />
      </van-cell-group>
      <van-button class="btn" round type="primary" size="large" @click="confirm"
        >确定</van-button
      >
    </div>
  </div>
</template>

<script>
export default {
  name: "ChangePassword",
  data() {
    return {
      usedPassword: "",
      newPassword: "",
    };
  },
  methods: {
    back() {
      this.$router.go(-1);
    },
    confirm() {
        if(this.usedPassword == ''){
            this.$notify({ type: "danger", message: '旧密码不能为空'});
            return;
        }else if(this.newPassword == ''){
            this.$notify({ type: "danger", message: '请输入新密码' });
            return
        }
        this.axios({
            method:'post',
            url:'/updatePassword',
            data:{
                password:this.newPassword,
                oldPassword:this.usedPassword
            }
        }).then(result=>{

            if(result.data.code == 700){
                this.$dialog
            .confirm({
              title: '你尚未登录',
              message: "请先登录后再试",
            })
            .then(() => {
              return this.$router.push({ name: "Login" });
            })
            .catch(() => {});
            }

            if(result.data.code == 'E003'){
                this.$notify({ type: "danger", message: result.data.msg });
            }
            if(result.data.code == 'E001'){
                 this.$dialog
            .confirm({
              title: result.data.msg,
              message: "请重新登录",
            })
            .then(() => {
              return this.$router.push({ name: "Login" });
            })
            .catch(() => {
                return this.$router.push({ name: "My" });
            });
            }
        })
    },
  },
};
</script>

<style lang="less" scoped>
.changePassword {
  padding-top: 76px;
  .van-nav-bar--fixed{
    padding-top: 30px;
    background-color: #0022ab;
  }
  /deep/.van-nav-bar__title{
    color: #fff;
  }
  /deep/.van-icon{
    color: #fff;
  }
  /deep/.van-nav-bar__text{
    color: #fff;
  }
  .content {
    text-align: center;
    .btn {
      width: 90%;
      margin-top: 10px;
    }
  }
}
</style>